import {useRouter} from "vue-router";
import type {RouteRecordName,RouteRecordRaw,Router} from "vue-router";
import type  {IRouter} from "@/mytype";

// 1、定义所有的 菜单和路由配置。
//  下面这个结构：是按照菜单结构，附带路由配置
let allMenusWithRoutes:Array<IRouter> = [
    {
        key: "0-0",
        label: "系统首页",
        path: "SystemHome",
        name: "SystemHome",
        component: () => import("../views/admin/SystemHome.vue"),
    },
    {
        key: "0-1",
        label: "轮播图管理",
        children: [
            {
                key: "0-1-0",
                label: "轮播图列表",
                path: "Banner",
                name: "Banner",
                component: () => import("../views/admin/Banner/Banner.vue")
            }
        ]
    },
    {
        key: "0-2",
        label: "产品信息",
        children: [
            {
                key: "0-2-0",
                label: "产品列表",
                path: "ProList",
                name: "ProList",
                component: () => import("../views/admin/Pro/ProList.vue")
            },
            {
                key: "0-2-1",
                label: "秒杀列表",
                path: "ProSecondSkillList",
                name: "ProSecondSkillList",
                component: () => import("../views/admin/Pro/ProSecondSkillList.vue")
            },
            {
                key: "0-2-2",
                label: "推荐列表",
                path: "ProSecondRecommend",
                name: "ProSecondRecommend",
                component: () => import("../views/admin/Pro/ProRecommend.vue")
            }
        ]
    },
    {
        key: "0-3",
        label: "账户管理",
        children: [
            {
                key: "0-3-0",
                label: "用户列表",
                path: "/user"
            },
            {
                key: "0-3-1-0",
                label: "管理员列表",
                path: "AdminList",
                name: "AdminList",
                component: () => import("../views/admin/Admin/AdminList.vue")
            }
        ]
    }
];

// 2、根据后端返回来的权限（checkedKeys） ,过滤第一步的数据
// checkedkeys: ["0-0", "0-1", "0-1-0", "0-1-1", "0-1-2", "0-3-1-0",]

export function getMenus($router:Router):Array<IRouter>{
    console.log("getMenu()");
    
    let checkedKeys = JSON.parse((sessionStorage.getItem("checkedKeys") as string));
    $router = $router || useRouter();

    // console.log("checkedKeys",checkedKeys);

    let result:Array<IRouter>;

    result = allMenusWithRoutes.filter(item=>{
        if(item.children){
            // 1、判断孩子有没有被选中的（有没有子一级的权限）
            let childenMenus = [];//保存所有被选中的孩子
            for(let i=0;i<item.children.length;i++){
                if(checkedKeys.includes(item.children[i].key)){
                    $router.addRoute(("Admin" as RouteRecordName),(item.children[i] as RouteRecordRaw));
                    childenMenus.push({...item.children[i]});
                }
            }
            // childenMenus.length>0：说明 有子一级被选中。
            // 如果有子一级被选中，那么父级菜单也需要加上
            if(childenMenus.length>0){
                // 解构出父级
                let tempItem = {...item};
                // 给父级的children赋值为选中的子一级
                tempItem.children = childenMenus;
                // 把父级返回。
                return tempItem;
            }
        }else{
            if(checkedKeys.includes(item.key)){
                $router.addRoute(("Admin" as RouteRecordName),item);
               return {...item};
            }    
        }
    });

    console.log("getMenus函数内部：result",result);
    console.log("getMenus函数内部：$router.getRoutes()",$router.getRoutes());
    
    return result;
    
}